package com.wjjumpad.cwjjumpad;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.wjjumpad.cwjjumpad.base.QLBaseActivity;
import com.wjjumpad.cwjjumpad.module.InstalledAppListActivity;
import com.wjjumpad.cwjjumpad.service.JumpAdSkipService;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends QLBaseActivity {

    private Toolbar toolbar;

    private TextView titleText;

    private TextView qidongStatusText;
    private Button btnOpen;

    private Button btnOpenAppList;

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void init(){
//        setTitle("123124aweqw");
////        setContentView(R.layout.activity_main);
//        findViews();
//        initViews();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        Window window = getWindow();
        //设置导航栏颜色
//        window.setNavigationBarColor(ContextCompat.getColor(this,R.color.cyan));
//        toolbar = (Toolbar) findViewById(R.id.toolbar);
//        action_bar_title
        setContentView(R.layout.activity_main);

//        setActionTitle("Skip AD Management");
        setActionTitle("跳过广告管理");
        hideBack();

        //主标题
//        toolbar.setTitle("主标题");
//        toolbar.setVisibility(View.VISIBLE);
//        toolbar = (Toolbar) findViewById(R.id.toolbar);
//        action_bar_back = (ImageView) findViewById(R.id.action_bar_back);
//        // action_bar_right = (ImageView) findViewById(R.id.action_bar_right);
//        actionBarTitle = (TextView) findViewById(R.id.action_bar_title);
//        actionBarRightBtn = (TextView) findViewById(R.id.action_bar_right_btn);
//        actionBarLine = findViewById(R.id.action_bar_line);
//        setContentView(R.layout.activity_main);


//        Log.v("LogUtil - onCreate",  String.valueOf(JumpAdSkipService.isServiceRunning()));
        findViews();
        initViews();
    }

    @Override
    public View initSuccessView() {
        return null;
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (qidongStatusText != null){
            // 检查AccessibilityService是否开启
            qidongStatusText.setText(JumpAdSkipService.isServiceRunning() ? "已开启" : "未开启");
            Log.v("LogUtil - onResume",  String.valueOf(JumpAdSkipService.isServiceRunning()));
        }


    }

    private void findViews(){
//        toolbar = (Toolbar) findViewById(R.id.toolbar);
//        action_bar_back = (ImageView) findViewById(R.id.action_bar_back);
//        // action_bar_right = (ImageView) findViewById(R.id.action_bar_right);
//        actionBarTitle = (TextView) findViewById(R.id.action_bar_title);
//        actionBarRightBtn = (TextView) findViewById(R.id.action_bar_right_btn);
//
//        toolbar.setVisibility(View.VISIBLE);

//        setActionTitle("123124aweqw");

//        toolbar.setVisibility(View.VISIBLE);


        titleText = findViewById(R.id.server_title);

        qidongStatusText = findViewById(R.id.qidong_status);
//        if (JumpAdSkipService.isServiceRunning()){
//            qidongStatusText.setText("已开启");
//        }else{
//            qidongStatusText.setText("未开启");
//        }
        btnOpen = findViewById(R.id.btn_open);
        if (JumpAdSkipService.isServiceRunning()){
            btnOpen.setVisibility(View.GONE);
        }else{
            btnOpen.setVisibility(View.VISIBLE);
        }
//        if (JumpAdSkipService.isServiceRunning()){
//            btnOpen.setVisibility(View.GONE);
//        }else{
//            btnOpen.setVisibility(View.VISIBLE);
//        }


        btnOpenAppList = findViewById(R.id.btn_open_app_list);

    }

    private void initViews() {
        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //开启服务
                showOpenAccessibilityDialog();


//                Resources resources = getResources();
//                String mainTabs[] = resources.getStringArray(R.array.main_tab_arrays);
//                TabPagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager(), mainTabs);
//                initBottomTab(adapter);
            }
        });


        btnOpenAppList.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Log.d("btnOpenAppList","进入应用列表");

//                Intent intent = new Intent();
//                intent.setClass(MainActivity.this, InstalledAppListActivity.class);
//                startActivity(intent);
//                List list = new Map();
//                Map m1 = new HashMap();
//                m1.put("name","1");
//                m1.put("name2","2");
//                m1.put("name3","3");
//                m1.put("name4","4");
//                m1.put("name5","5");
//
//                Log.d("", "onClick: ");

//                InstalledAppListActivity.startActivity(MainActivity.this,m1);
                start(InstalledAppListActivity.class);
//                Intent intent = new Intent(MainActivity.this,InstalledAppListActivity.class);
//                startActivity(intent);
            }

        });
    }

    /**
     * 打开无障碍权限的提示
     * */
    private void showOpenAccessibilityDialog(){
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setMessage("依次点击：无障碍->已下载应用->CWJNEW跳过助手")
                .setPositiveButton("取消", null)
                .setNeutralButton("好的", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //跳转至系统界面
                        Intent intent_abs = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                        intent_abs.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent_abs);
                    }
                })
                .create();
        dialog.show();
    }

    /**
     * 设置底部按钮
     * */
//    private void initBottomTab(TabPagerAdapter adapter) {
//        for (int i = 0; i < adapter.getCount(); i++) {
//            TabLayout.Tab tab = mainTabLayout.getTabAt(i);//获得每一个tab
//            if (tab != null) {
//                tab.setCustomView(R.layout.view_main_tab);//给每一个tab设置view
//                View customView = tab.getCustomView();
//                if (customView != null) {
//                    TextView textView = customView.findViewById(R.id.tv_home_tab);
//                    textView.setText(adapter.getPageTitle(i));//设置tab上的文字
//                    int resId = R.drawable.selector_main_tab_home;
//                    switch (i) {
//                        case HOME:
//                            textView.setSelected(true);//第一个tab被选中
//                            resId = R.drawable.selector_main_tab_home;
//                            break;
//                        case MARKET:
//                            resId = R.drawable.selector_main_tab_market;
//                            break;
//                        case EXCHANGE:
//                            resId = R.drawable.selector_main_tab_trade;
//                            break;
//                        case MINE:
//                            resId = R.drawable.selector_main_tab_mine;
//                            break;
//                    }
//                    Drawable tabImg = getDrawable(resId);
//                    tabImg.setBounds(0, 0, 80,62);
//                    textView.setCompoundDrawables(null, tabImg, null, null);  //设置到哪个控件的位置（）
//                }
//            }
//        }
//    }
}